package com.ngu.sys.user.services.Impl;

import com.ngu.sys.user.dao.TSysUserMapper;
import com.ngu.sys.user.model.TSysUser;
import com.ngu.sys.user.services.SysUserService;
import com.ngu.util.AjaxResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
* @Description:    用户操作逻辑层
* @Author:         hq
* @CreateDate:     2019/9/28 2019/9/28
* @UpdateUser:     hq
* @UpdateDate:     2019/9/28 2019/9/28
* @UpdateRemark:   修改内容
* @Version:        1.0
*/
@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private TSysUserMapper tSysUserMapper;

    @Override
    public Object findSysUserList(TSysUser user) {
        List<TSysUser>  userList = tSysUserMapper.selectList(user);
        if(ObjectUtils.isEmpty(userList)){
            return  "没有数据";
        }else{
            return userList;
        }
    }

    /**
    * 登录
    * @author      hq
    * @date        2019/9/28 16:42
    */
    @Override
    public AjaxResponse userLogin(TSysUser user, HttpSession session) {
        List<TSysUser>  data = tSysUserMapper.selectByUNameOrPwd(user);

        if(ObjectUtils.isEmpty(data)){
            return  AjaxResponse.success("用户名或密码错误","99999",data);
        }else{
            //隐藏密码
            data.get(0).setPassword("****");
            session.setAttribute("SysUser",data);
            return  AjaxResponse.success("登录成功!","00000",data);
        }
    }

    /**
    * 退出登录 清空session
    * @author      hq
    * @date        2019/10/2 18:52
    */
    @Override
    public AjaxResponse loginOut(HttpSession session) {
        //让该用户的session失效
        session.removeAttribute("SysUser");
        return AjaxResponse.success("ok","200");
    }
}
